package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.DialogHIstoryBean;
import com.example.hotel_lxy.bean.DialogReserveBean;
import com.example.hotel_lxy.bean.StoreDateDialogBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.ui.activity.ZeroPointActivity;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * 历史订单
 * create by Sincerly on 9999/9/9 0009
 **/
public class HistoryOrderDialog extends Dialog {
    private Context mContext;
    private RecyclerView dialog_rv_view;
    private  static String year="";
    private  static String month="";
    private  static String day="";
    private  static String qid="";
    private  static String eid="";
    private  static String noon="";

    private List<DialogHIstoryBean.DataBean.ListBean> listBeans=new ArrayList<>();



    public HistoryOrderDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_history_order, null);
        dialog_rv_view = view.findViewById(R.id.dialog_rv_view);
        TextView text_gb=view.findViewById(R.id.text_gb);
        text_gb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        showDataRv();
        return view;
    }

    private void showDataRv() {
            Log.e("TAG",year+"  "+month+"  "+day+"  "+qid+"  "+eid+"  "+noon);
        OkHttpUtils.post()
                .url(Api.POST_ZERO_HISTORY)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("year",year)
                .addParams("month",month)
                .addParams("day",day)
                .addParams("qid",qid)
                .addParams("eid",eid)
                .addParams("noon",noon)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "零点历史信息onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "零点历史信息onResponse   " + response);

                        DialogHIstoryBean dialogHIstoryBean = JsonUtils.parseByGson(response, DialogHIstoryBean.class);
                        BeanBean beanBean=JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("0")){
                            dialog_rv_view.setLayoutManager(new LinearLayoutManager(mContext));
                            if(dialogHIstoryBean.getData().getList()==null){

                                return;
                            }
                            List<DialogHIstoryBean.DataBean.ListBean> list = dialogHIstoryBean.getData().getList();

                            RBaseAdapter<DialogHIstoryBean.DataBean.ListBean> storeDateDialogBeanRBaseAdapter=new RBaseAdapter<DialogHIstoryBean.DataBean.ListBean>(mContext,
                                    R.layout.dialog_item_history_order,list) {
                                @Override
                                protected void fillItem(RViewHolder holder,DialogHIstoryBean.DataBean.ListBean item, int position) {
                                    TextView text_elegant=holder.itemView.findViewById(R.id.text_elegant);
                                    TextView text_name=holder.itemView.findViewById(R.id.text_name);
                                    TextView text_mobile=holder.itemView.findViewById(R.id.text_mobile);
                                    TextView text_people=holder.itemView.findViewById(R.id.text_people);
                                    TextView text_end_time=holder.itemView.findViewById(R.id.text_end_time);
                                    TextView text_content=holder.itemView.findViewById(R.id.text_content);
                                    TextView text_yu_name=holder.itemView.findViewById(R.id.text_yu_name);
                                    TextView text_addtime=holder.itemView.findViewById(R.id.text_addtime);
                                    text_elegant.setText(""+item.getElegant());
                                    if(item.getSex().equals("1")){
                                        text_name.setText(""+item.getName()+"先生");
                                    }else{
                                        text_name.setText(""+item.getName()+"女士");
                                    }
                                    text_name.setText(""+item.getName());
                                    text_mobile.setText(""+item.getMobile());
                                    text_people.setText(""+item.getPeople());
                                    text_end_time.setText(""+item.getEnd());
                                    text_content.setText(""+item.getContent());
                                    text_yu_name.setText(""+item.getDirector());
                                    text_addtime.setText(""+item.getAddtime());
                                }

                                @Override
                                protected int getViewType(DialogHIstoryBean.DataBean.ListBean item, int position) {
                                    return 0;
                                }
                            };
                            dialog_rv_view.setAdapter(storeDateDialogBeanRBaseAdapter);
                        }
                        else  if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }
                        else{
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }
                    }
                });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 9 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static HistoryOrderDialog show(Context context, OnDialogClickListener listener,String years,String months,String days,
                                          String qids,String eids,String noons) {
        HistoryOrderDialog dialog = new HistoryOrderDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        year=years;
        month=months;
        day=days;
        qid=qids;
        eid=eids;
        noon=noons;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public HistoryOrderDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }
}